package com.ruoyi.controller;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.teaopenapi.models.Config;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.model.Member;
import com.ruoyi.service.MemberService;
import com.ruoyi.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author:Jiang
 * @Date: 2023/08/11/ 9:52
 **/
@RestController
@RequestMapping("/sellerlogin")
public class SellerLoginController {
    @Autowired
    private MemberService memberService;
    @PostMapping("/login")
    public ResponseResult login(@RequestBody Member member){
        Map<String,Object> map = memberService.login(member);
//        Map<String,Object> map=new HashMap<>();
//        map.put("token",token);
        ResponseResult responseResult=new ResponseResult<>(200,"登陆成功",map);
        return responseResult;
    }
    @GetMapping("/loginout")
    public ResponseResult login(){
        Boolean result = memberService.logout();
        if(result){
            return new ResponseResult(200,"退出登录成功");
        }else {
            return new ResponseResult(0,"退出登陆失败");
        }
    }
    @GetMapping("/checkRegCode")
    public ResponseResult checkRegCode(String tel) throws Exception {
        Config config = new Config()
                //这里修改为我们上面生成自己的AccessKey ID
                .setAccessKeyId("LTAI5tEY6F6RxQZZ4kTWbZDW")

                //这里修改为我们上面生成自己的AccessKey Secret
                .setAccessKeySecret("4tzh8DmjvPAsAMZ3lLLUx4K6RTpkh1");
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        Client client = new Client(config);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("阿里云短信测试")//短信签名
                .setTemplateCode("SMS_154950909")//短信模板
                .setPhoneNumbers(tel)//这里填写接受短信的手机号码
                .setTemplateParam("{\"code\":\"1234\"}");//验证码
        // 复制代码运行请自行打印 API 的返回值
        client.sendSms(sendSmsRequest);
        return new ResponseResult(200,"1234");
    }
    /**
     * 新增#商家信息
     */

    @PostMapping("/add")
    public ResponseResult add(@RequestBody Member member)
    {
        int i = memberService.insertMember(member);
        ResponseResult responseResult=new ResponseResult<>(500,"注册失败");

        if (i == 1){
            responseResult=new ResponseResult<>(200,"注册成功");
        }
        return responseResult;
    }

}
